class OfxListView < Gtk::VBox
  def initialize(model, prefs)
    super()
    @model = model
    @prefs = prefs
    
    @store = Gtk::ListStore.new(String)
    row = @store.insert(0)
    @model.ofx.each do |k,v|
      row[0] = k
      row = @store.insert_after(row)
    end
    #row[0] = "Import OFX File..."
    
    @view = Gtk::TreeView.new(@store)
    @view.append_column(
      Gtk::TreeViewColumn.new("OFX Files", Gtk::CellRendererText.new, :text => 0)
    )
    self.pack_start(@view)
    
    @select_ofx_file = proc {|x| puts "Selected OFX file #{x}"}
    @view.signal_connect("row-activated") do |view, path, column|
      if iter = view.model.get_iter(path)
        @select_ofx_file.call(iter[0])
      end
    end
  end
  
  def on_select_ofx_file(&block)
    @select_ofx_file = block
  end
end  


class OfxFileView < Gtk::VBox
  def initialize(ofx)
    super()

    self.pack_start(Gtk::Label.new("OFX tx for #{ofx.statement.account_id}"), false, false, 0)
    
    @store = Gtk::ListStore.new(String, String, String, String, String, String)
    row = @store.insert(0)
    ofx.statement.transactions.each do |tx|
      row[0] = tx.transaction_id
      row[1] = tx.transaction_type
      row[2] = tx.date_posted.to_s
      row[3] = tx.check_number
      row[4] = tx.name
      row[5] = tx.amount
      row = @store.insert_after(row)
    end

    @view = Gtk::TreeView.new(@store)
    @view.append_column(Gtk::TreeViewColumn.new("Tx ID", Gtk::CellRendererText.new, :text => 0))
    @view.append_column(Gtk::TreeViewColumn.new("Tx Type", Gtk::CellRendererText.new, :text => 1))
    @view.append_column(Gtk::TreeViewColumn.new("Date Posted", Gtk::CellRendererText.new, :text => 2))
    @view.append_column(Gtk::TreeViewColumn.new("Check #", Gtk::CellRendererText.new, :text => 3))
    @view.append_column(Gtk::TreeViewColumn.new("Name", Gtk::CellRendererText.new, :text => 4))
    @view.append_column(Gtk::TreeViewColumn.new("Amount", Gtk::CellRendererText.new, :text => 5))
    @scroller = Gtk::ScrolledWindow.new
    @scroller.add(@view)
    self.pack_start(@scroller, true, true, 0)

  end
end

class OfxPage < Gtk::VBox
  def initialize(model, prefs)
    super()
    @model = model
    @prefs = prefs
    @toolbar = build_toolbar
    self.pack_start(@toolbar, false, false, 0)
    @panes = build_panes
    self.pack_start(@panes, true, true, 0)
  end

  def build_panes
    list_view = OfxListView.new(@model, @prefs)
    list_frame = Gtk::Frame.new()
    list_frame.add(list_view)

    file_view = OfxFileView.new(@model.ofx[@model.ofx.keys[0]])
    file_frame = Gtk::Frame.new()
    file_frame.add(file_view)
    file_frame.shadow_type = Gtk::SHADOW_ETCHED_IN
    
    list_view.on_select_ofx_file do |ofx|
      file_frame.each{|c| file_frame.remove(c)}
      file_view = OfxFileView.new(@model.ofx[ofx])
      file_frame.add(file_view)
      file_frame.show_all
    end

    panes = Gtk::HPaned.new
    panes.position = @prefs[:ofx_page_list_width]
    panes.pack1(list_frame, false, true)
    panes.pack2(file_frame, true, true)
    return panes
  end
  
  def build_toolbar
    toolbar = Gtk::Toolbar.new
    toolbar.tooltips = true
    import = Gtk::ToolButton.new(Icons.get_icon(:open, @prefs[:tool_icon_size]), "Open OFX")
    toolbar.append(import, "Open OFX File")
    return toolbar
  end
  
end
